package daos.restaurant.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import model.entities.Restaurant;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import daos.AbstractDAO;
import daos.restaurant.RestaurantAppDAO;

public class RestaurantAppDAOJdbcTemplateImpl extends AbstractDAO implements RestaurantAppDAO {

    private JdbcTemplate jdbcTemplate;
    
    public void insert(Restaurant restaurant) throws SQLException {
        String insertSql = "INSERT INTO RESTAURANTS (NAME, CITY) VALUES (?,?)";
        Object[] parameters = { restaurant.getName(), restaurant.getCity() };
        jdbcTemplate.update(insertSql, parameters);
        
        System.out.println(insertSql);
        System.out.println("Row inserted OK");
    }
    
    public void delete(Restaurant restaurant) throws SQLException {
        String deleteSql = "DELETE FROM RESTAURANTS WHERE NAME = ?";
        jdbcTemplate.update(deleteSql, restaurant.getName());
        
        System.out.println(deleteSql);
        System.out.println("Row deleted OK");
    }

    public void update(Restaurant restaurant) throws SQLException {
        String updateSql = "UPDATE RESTAURANTS SET CITY = ? WHERE NAME = ?";
        Object[] parameters = { restaurant.getCity(), restaurant.getName() };
        jdbcTemplate.update(updateSql, parameters);
        
        System.out.println(updateSql);
        System.out.println("Row updated OK");
    }

    public List<Restaurant> selectAll() throws SQLException {
        String selectSql = "SELECT * FROM RESTAURANTS";
        List<Restaurant> restaurantSeleccionados = jdbcTemplate.query(selectSql,
                getRowMapper());

        System.out.println(selectSql);
        System.out.println("Row selected OK");

        return restaurantSeleccionados;
    }
    
    public List<Restaurant> selectByName(Restaurant restaurant) throws SQLException {
        String selectSql = "SELECT NAME, CITY FROM RESTAURANTS WHERE UPPER(NAME) LIKE ?";
        List<Restaurant> restaurantSeleccionados = jdbcTemplate.query(selectSql, getRowMapper(), "%" + restaurant.getName().toUpperCase() + "%");
        
        System.out.println(selectSql);
        System.out.println("Row selected OK");
        
        System.out.println(restaurantSeleccionados);
        
        return restaurantSeleccionados;
    }

    public boolean exist(Restaurant restaurant) throws SQLException {
        String selectSql = "SELECT * FROM RESTAURANTS WHERE NAME = ?";
        return !jdbcTemplate.query(selectSql, getRowMapper(), restaurant.getName()).isEmpty();
    }
    
    public RowMapper<Restaurant> getRowMapper() {
        return new RowMapper<Restaurant>() {
            public Restaurant mapRow(ResultSet rs, int arg1) throws SQLException {
                Restaurant restaurantSeleccionado = new Restaurant();

                restaurantSeleccionado.setName(rs.getString("NAME"));
                restaurantSeleccionado.setCity(rs.getString("CITY"));

                return restaurantSeleccionado;
            }
        };
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}